﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Custom.Generic;
using System.Drawing;
using System.Linq;
using Gizmox.WebGUI.Common.Extensibility;
using Gizmox.WebGUI.Forms;
using Gizmox.WebGUI.Forms.Design;
using Gizmox.WebGUI.Forms.Skins;

namespace CoCoon.Forms.Ribbon
{
    [Serializable]
    [ToolboxItem(false)]
    public class RibbonBarPushButtonItem : RibbonBarButtonItem
    {
        public RibbonBarPushButtonItem()
            : base()
        {
            Button.Click += ButtonClick;
        }

        private void ButtonClick(object sender, EventArgs e)
        {
            if (Pushed && PushGroup != null) return;
            SetGroup();
        }

        public event EventHandler<EventArgs<bool>> PushChanged
        {
            add { Button.PushedChanged += value; }
            remove { Button.PushedChanged -= value; }
        }

        public string PushGroup { get; set; }

        public PushButton Button
        {
            get { return this.Control as PushButton; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="ToolBarButton"/> is pushed.
        /// </summary>
        /// <value><c>true</c> if pushed; otherwise, <c>false</c>.</value>
        ///
        [DefaultValue(false)]
        public bool Pushed
        {
            get { return Button.Pushed; }
            set { Button.Pushed=value; }
        }

        private List<RibbonBarPushButtonItem> PushGroupButtons
        {
            get
            {
                var l = new List<RibbonBarPushButtonItem>();
                var container = this.Owner() as RibbonBarItemContainer;
                if (container != null)
                {
                    l = container.Items.Where(i => i is RibbonBarPushButtonItem)
                                 .Cast<RibbonBarPushButtonItem>()
                                 .Where(i => i.PushGroup != null && i.PushGroup.Equals(PushGroup))
                                 .ToList();
                }
                return l;
            }
        }

        private void SetGroup()
        {
            foreach (var item in PushGroupButtons)
            {
                item.Pushed = item.Equals(this);
            }
            this.Owner().Control.Update();
        }

        protected override Button CreateButton()
        {
              var b = new PushButton();
            b.CustomStyle = "RibbonBarPushBig";
            return b;          
        }


    }
}
